package com.dc.toolkit.hutool.core;

import cn.hutool.core.util.NumberUtil;
import org.junit.Test;

import java.math.BigDecimal;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Description: Hutool NumberUtil Demo
 * @author: gdc
 * @date: 2021/4/24
 * @version 1.0
 */
public class NumberUtilTest {

    /**
     * 测试       add()       加
     */
    @Test
    public void testAdd(){
        double a = NumberUtil.add(1, 2);
        System.out.println(a);

        BigDecimal b = NumberUtil.add("1", "4");
        System.out.println(b);
    }

    /**
     * 测试       sub()       减
     */
    @Test
    public void testSub(){
        double a = NumberUtil.sub(1, 2);
        System.out.println(a);

        BigDecimal b = NumberUtil.sub("1", "4");
        System.out.println(b);
    }

    /**
     * 测试       mul()       乘
     */
    @Test
    public void testMul(){
        double a = NumberUtil.mul(1, 2);
        System.out.println(a);

        BigDecimal b = NumberUtil.mul("1", "4");
        System.out.println(b);
    }

    /**
     * 测试       mul()       乘
     */
    @Test
    public void testDiv(){
        double a = NumberUtil.div(1, 2);
        System.out.println(a);

        BigDecimal b = NumberUtil.div("1", "4");
        System.out.println(b);
    }

    /**
     * 测试       formatPercent()     数据设置为百分数
     */
    @Test
    public void testFormatPercent(){
        String result = NumberUtil.formatPercent(0.123456, 2);
        System.out.println(result);
    }

    /**
     * 测试   NumberUtil.toBigDecimal()       转换为 BigDecimal 类型
     */
    @Test
    public void testToBigDecimal(){
        assertThat(BigDecimal.valueOf(10), equalTo(NumberUtil.toBigDecimal("10")));
        assertThat(BigDecimal.valueOf(10), equalTo(NumberUtil.toBigDecimal(10)));
        assertThat(BigDecimal.valueOf(10), equalTo(NumberUtil.toBigDecimal(10.0)));
    }

    /**
     * 测试   range() 给定数据的范围，步进 + 1
     */
    @Test
    public void testRange(){
        int[] rangeA = NumberUtil.range(10);
        for (int i : rangeA) {
            System.out.print(i + "\t");
        }

        System.out.println();

        int[] rangeB = NumberUtil.range(15, 20);
        for (int i : rangeB) {
            System.out.print(i + "\t");
        }

        System.out.println();

        int[] rangeC = NumberUtil.range(1, 20, 3);
        for (int i : rangeC) {
            System.out.print(i + "\t");
        }
    }

    /**
     * 测试  比较两个数大小
     */
    @Test
    public void testCompany(){
        assertThat(NumberUtil.compare(1, 10), equalTo(-1));
        assertThat(NumberUtil.compare(10, 10), equalTo(0));
        assertThat(NumberUtil.compare(10, 1), equalTo(1));

        assertThat(NumberUtil.compare(1.0, 10.1), equalTo(-1));
        assertThat(NumberUtil.compare(10.2, 10.2), equalTo(0));
        assertThat(NumberUtil.compare(10.3, 1), equalTo(1));
    }

    /**
     * 测试  数据是否相等
     */
    @Test
    public void testEquals(){
        assertThat(true, equalTo(NumberUtil.equals(1, 1)));
        assertThat(true, equalTo(NumberUtil.equals(1.2F, 1.2F)));
        assertThat(true, equalTo(NumberUtil.equals(1.2D, 1.2D)));
        assertThat(true, equalTo(NumberUtil.equals(BigDecimal.ONE, BigDecimal.ONE)));
    }

    /**
     * 测试 数据是否大于
     */
    @Test
    public void testIsGreater(){
        assertThat(NumberUtil.isGreater(BigDecimal.valueOf(10), BigDecimal.valueOf(5)), equalTo(true));
        assertThat(NumberUtil.isGreater(BigDecimal.ONE, BigDecimal.ONE), equalTo(false));

        assertThat(NumberUtil.isGreaterOrEqual(BigDecimal.ONE, BigDecimal.ONE), equalTo(true));
    }

    /**
     * 测试 数据是否小于
     */
    @Test
    public void testIsLess(){
        assertThat(NumberUtil.isLess(BigDecimal.valueOf(10), BigDecimal.valueOf(5)), equalTo(false));
        assertThat(NumberUtil.isLess(BigDecimal.ZERO, BigDecimal.ONE), equalTo(true));

        assertThat(NumberUtil.isLess(BigDecimal.ONE, BigDecimal.ONE), equalTo(false));
        assertThat(NumberUtil.isLessOrEqual(BigDecimal.ONE, BigDecimal.ONE), equalTo(true));
    }

    /**
     * 测试  parse 解析字符串为 数字类型
     */
    @Test
    public void testParseNumber(){
        assertThat(10, equalTo(NumberUtil.parseInt("10")));
        assertThat(10L, equalTo(NumberUtil.parseLong("10")));
        assertThat(10F, equalTo(NumberUtil.parseFloat("10")));
        assertThat(10D, equalTo(NumberUtil.parseDouble("10")));
    }

    /**
     * 测试 获取最大值
     */
    @Test
    public void testMax(){
        assertThat(NumberUtil.max(1, 2, 5, 10, 7), equalTo(10));
        assertThat(NumberUtil.max(1.0, 2.0, 5.0, 10.3, 7.3), equalTo(10.3));
        assertThat(NumberUtil.max(BigDecimal.ONE, BigDecimal.ZERO, BigDecimal.TEN), equalTo(BigDecimal.TEN));
    }

    /**
     * 测试 获取最小值
     */
    @Test
    public void testMin(){
        assertThat(NumberUtil.min(1, 2, 5, 10, 7), equalTo(1));
        assertThat(NumberUtil.min(1.0, 2.0, 5.0, 10.3, 7.3), equalTo(1.0));
        assertThat(NumberUtil.min(BigDecimal.ONE, BigDecimal.ZERO, BigDecimal.TEN), equalTo(BigDecimal.ZERO));
    }
    
    
    /**
     * 测试 round()保留位数 ,并 四舍五入
     */
    @Test
    public void testRound(){
        double d = 1.23456;
        assertThat(NumberUtil.round(d, 2), equalTo(BigDecimal.valueOf(1.23)));
        assertThat(NumberUtil.round(d, 3), equalTo(BigDecimal.valueOf(1.235)));

        BigDecimal bd = BigDecimal.valueOf(1.23456);
        assertThat(NumberUtil.round(bd, 2), equalTo(BigDecimal.valueOf(1.23)));
        assertThat(NumberUtil.round(bd, 3), equalTo(BigDecimal.valueOf(1.235)));

        String str = "1.23456";
        assertThat(NumberUtil.round(str, 2), equalTo(BigDecimal.valueOf(1.23)));
        assertThat(NumberUtil.round(str, 3), equalTo(BigDecimal.valueOf(1.235)));
    }

    /**
     * 测试 round()保留位数 ,并 四舍五入
     */
    @Test
    public void testRoundStr(){
        double d = 1.23456;
        assertThat(NumberUtil.roundStr(d, 2), equalTo("1.23"));
        assertThat(NumberUtil.roundStr(d, 3), equalTo("1.235"));

        String str = "1.23456";
        assertThat(NumberUtil.roundStr(str, 2), equalTo("1.23"));
        assertThat(NumberUtil.roundStr(str, 3), equalTo("1.235"));
    }

    /**
     * 测试
     */
    @Test
    public void testIs(){
        assertThat(NumberUtil.isInteger("1"), equalTo(true));
        assertThat(NumberUtil.isInteger("1.3"), equalTo(false));
        assertThat(NumberUtil.isInteger("abc"), equalTo(false));

        assertThat(NumberUtil.isLong("12"), equalTo(true));
        assertThat(NumberUtil.isLong("12.3"), equalTo(false));
        assertThat(NumberUtil.isLong("abc"), equalTo(false));

        assertThat(NumberUtil.isDouble("12"), equalTo(false));
        assertThat(NumberUtil.isDouble("12.3"), equalTo(true));
        assertThat(NumberUtil.isDouble("abc"), equalTo(false));
    }

    /**
     * 测试 生成随机数字
     */
    @Test
    public void testGenerateRandomNumber(){
        int[] result = NumberUtil.generateRandomNumber(0, 100, 10);
        for (int i : result) {
            System.out.println(i);
        }
    }

    /**
     * 测试
     */
    @Test
    public void testgenerateBySet(){
        Integer[] result = NumberUtil.generateBySet(0, 12, 10);
        for (Integer i : result) {
            System.out.println(i);
        }
    }
    
    
    /**
     * BigDecimal 去除尾数中多余的0,并以普通方式展示
     *  方式：.stripTrailingZeros().toPlainString()
     *  说明：
     *      【1】.stripTrailingZeros()，去除多余的零，但是会使用科学计数法展示；
     *      【2】.toPlainString() 将数据以普通计数方式展示。
     *  总结：.stripTrailingZeros().toPlainString()：去除多余的零，同时以普通计数方式展示
     */
    @Test
    public void testBigDecimal_stripTrailingZeros_toPlainString(){

        BigDecimal b1 = new BigDecimal("123.45600000");
        System.out.println(b1 + "------" + b1.stripTrailingZeros() + "------" + b1.stripTrailingZeros().toPlainString());


        BigDecimal b2 = new BigDecimal("0.1230045000");
        System.out.println(b2 + "------" + b2.stripTrailingZeros() + "------" + b2.stripTrailingZeros().toPlainString());

        BigDecimal b3 = new BigDecimal("100000000000000");
        System.out.println(b3 + "------" + b3.stripTrailingZeros() + "------" + b3.stripTrailingZeros().toPlainString());
    }
}
